<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/21 0021
 * Time: 13:27
 */

namespace Home\Controller;


class BbsController extends CommonController
{
   public function index(){
       $num =  M('bbs_reply')->count(); //获取评论总数
       $this->assign('num',$num);
       $data=array();
       $data=$this->getCommlist();//获取评论列表
       $this->assign("commlist",$data);
       $titles=M('bbs_section')->where('status','1')->select();
       $hot = D('bbs_topic')->where('is_hot=1')->limit(5)->select();
       $bbs = D('bbs_topic'); // 实例化User对象
       $count      = $bbs->where('status=1')->count();// 查询满足要求的总记录数
       $Page       = new \Think\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(25)
       $show       = $Page->show();// 分页显示输出
       // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
       $map['status']=1;
       if(I('param.id')){
           $map['section_id']=I('param.id');
       }
       $list = $bbs->where($map)->order('ctime')->limit($Page->firstRow.','.$Page->listRows)->select();
       $this->assign('list',$list);// 赋值数据集
       $this->assign('page',$show);// 赋值分页输出
       $this->assign('titles',$titles);
       $this->assign('hot',$hot);
       $this->display();
   }

   public function add(){
       session_start();
       $user=session('user');

       $titles=M('bbs_section')->where('status','1')->select();
       $this->assign('titles',$titles);
       $this->assign('user',$user);

       $this->display();
   }
   public function baocun(){
       $bbs=D('bbs_topic');
       $data['title']=I('param.title');
       $data['section_id']=I('param.section_id');
       $data['user_id']=I('param.user_id');
       $data['content']=I('param.content');
       $data['ctime']=time();
       $res=$bbs->data($data)->add();
       $m3_result['status'] = 0;
       $m3_result['message'] = '添加成功';
       $this->ajaxReturn($m3_result);
   }

   public function detail(){
       $id=I('param.id');
       $map['id'] = $id;
       $article = M('bbs_topic')->where($map)->select();
       $this->assign('article',$article);
       $this->display();
   }

    protected function getCommlist($parent_id = 0,&$result = array()){
        $arr = M('bbs_reply')->where("topic_id = '".$parent_id."'")->order("ctime desc")->select();
        if(empty($arr)){
            return array();
        }
        foreach ($arr as $cm) {
            $thisArr=&$result[];
            $cm["children"] = $this->getCommlist($cm["id"],$thisArr);
            $thisArr = $cm;
        }
        return $result;
    }
}